--- title: Testing Diffusion Flow Embedder on Circle and Swiss Roll keywords: fastai sidebar: home_sidebar nb_path: "02d02a Testing Diffusion Flow Embedder on Circle and Swiss Roll.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
from directed_graphs.datasets import directed_circle, directed_cylinder, directed_spiral, directed_swiss_roll, directed_spiral_uniform, directed_swiss_roll_uniform
from directed_graphs.datasets import plot_directed_2d, plot_directed_3d
from directed_graphs.diffusion_flow_embedding import DiffusionFlowEmbedder
import torch
import numpy as np
{% endraw %} {% raw %}
if torch.__version__[:4] == "1.13":
	# device = torch.device('cuda' if torch.cuda.is_available() else 'mps' if torch.has_mps else 'cpu')
	device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

else:
	device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device
device(type='cuda')
{% endraw %}

Flat Circle

{% raw %}
X, flow, labels = directed_circle(num_nodes=300, radius=1)
plot_directed_2d(X, flow, labels)
{% endraw %} {% raw %}
X = torch.tensor(X)
flow = torch.tensor(flow)
X = X.float().to(device)
flow = flow.float().to(device)
{% endraw %} {% raw %}
dfe = DiffusionFlowEmbedder(X,
flow,
t=1,
sigma_graph=15,
sigma_embedding=15,
device=device, 
autoencoder_shape = [50,10],
flow_artist_shape=[30,20,10],
flow_strength_graph=2, 
flow_strength_embedding=5, 
learnable_flow_strength=True,
weight_of_flow = 0.5,
learning_rate=0.001
)
dfe = dfe.to(device)
embeddings = dfe.fit(n_steps=2000)
  0%|          | 0/2000 [00:00<?, ?it/s]/gpfs/ysm/project/sumry2022/sumry2022_gt392/conda_envs/test/lib/python3.9/site-packages/torch/functional.py:568: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at  /opt/conda/conda-bld/pytorch_1646756402876/work/aten/src/ATen/native/TensorShape.cpp:2228.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]
/gpfs/ysm/project/sumry2022/sumry2022_gt392/directed_graphs/directed_graphs/diffusion_flow_embedding.py:166: UserWarning: The use of `x.T` on tensors of dimension other than 2 to reverse their shape is deprecated and it will throw an error in a future release. Consider `x.mT` to transpose batches of matricesor `x.permute(*torch.arange(x.ndim - 1, -1, -1))` to reverse the dimensions of a tensor. (Triggered internally at  /opt/conda/conda-bld/pytorch_1646756402876/work/aten/src/ATen/native/TensorShape.cpp:2318.)
  x_smoothness = (x_vecs.T @ L @ x_vecs) #/ torch.linalg.norm(x_vecs)**2
EPOCH 0. Loss 2232.2802734375. Flow strength 4.999000072479248. Weight of flow 0.5 Heatmap of P embedding is 
  5%|▍         | 99/2000 [00:09<02:39, 11.93it/s]
EPOCH 100. Loss 2080.5830078125. Flow strength 4.904240131378174. Weight of flow 0.5 Heatmap of P embedding is 
 10%|▉         | 199/2000 [00:18<02:32, 11.84it/s]
EPOCH 200. Loss 1921.5621337890625. Flow strength 4.806856155395508. Weight of flow 0.5 Heatmap of P embedding is 
 15%|█▍        | 299/2000 [00:28<02:23, 11.88it/s]
EPOCH 300. Loss 1667.8922119140625. Flow strength 4.711354732513428. Weight of flow 0.5 Heatmap of P embedding is 
 20%|█▉        | 399/2000 [00:37<02:14, 11.88it/s]
EPOCH 400. Loss 1409.0606689453125. Flow strength 4.634890556335449. Weight of flow 0.5 Heatmap of P embedding is 
 25%|██▍       | 499/2000 [00:46<02:06, 11.87it/s]
EPOCH 500. Loss 1141.1136474609375. Flow strength 4.575681686401367. Weight of flow 0.5 Heatmap of P embedding is 
 30%|██▉       | 599/2000 [00:55<01:58, 11.85it/s]
EPOCH 600. Loss 923.66650390625. Flow strength 4.533069610595703. Weight of flow 0.5 Heatmap of P embedding is 
 35%|███▍      | 699/2000 [01:05<01:50, 11.82it/s]
EPOCH 700. Loss 828.336669921875. Flow strength 4.495057106018066. Weight of flow 0.5 Heatmap of P embedding is 
 40%|████      | 800/2000 [01:14<01:45, 11.35it/s]
EPOCH 800. Loss 601.1668701171875. Flow strength 4.457322597503662. Weight of flow 0.5 Heatmap of P embedding is 
 45%|████▌     | 900/2000 [01:23<01:32, 11.87it/s]
EPOCH 900. Loss 422.0808410644531. Flow strength 4.429161071777344. Weight of flow 0.5 Heatmap of P embedding is 
 50%|█████     | 1000/2000 [01:33<01:28, 11.34it/s]
EPOCH 1000. Loss 342.57403564453125. Flow strength 4.403886318206787. Weight of flow 0.5 Heatmap of P embedding is 
 55%|█████▌    | 1100/2000 [01:43<01:18, 11.41it/s]
EPOCH 1100. Loss 342.6728515625. Flow strength 4.381978511810303. Weight of flow 0.5 Heatmap of P embedding is 
 60%|██████    | 1200/2000 [01:54<01:28,  9.06it/s]
EPOCH 1200. Loss 391.9619445800781. Flow strength 4.358258247375488. Weight of flow 0.5 Heatmap of P embedding is 
 65%|██████▍   | 1299/2000 [02:04<01:10,  9.91it/s]
EPOCH 1300. Loss 259.33984375. Flow strength 4.341099739074707. Weight of flow 0.5 Heatmap of P embedding is 
 70%|██████▉   | 1399/2000 [02:14<00:50, 11.85it/s]
EPOCH 1400. Loss 241.97116088867188. Flow strength 4.331248760223389. Weight of flow 0.5 Heatmap of P embedding is 
 75%|███████▍  | 1499/2000 [02:24<00:42, 11.82it/s]
EPOCH 1500. Loss 285.223876953125. Flow strength 4.3260626792907715. Weight of flow 0.5 Heatmap of P embedding is 
 80%|███████▉  | 1599/2000 [02:33<00:33, 11.86it/s]
EPOCH 1600. Loss 337.34283447265625. Flow strength 4.3230414390563965. Weight of flow 0.5 Heatmap of P embedding is 
 85%|████████▍ | 1699/2000 [02:42<00:25, 11.84it/s]
EPOCH 1700. Loss 312.47235107421875. Flow strength 4.3199968338012695. Weight of flow 0.5 Heatmap of P embedding is 
 90%|████████▉ | 1799/2000 [02:52<00:17, 11.80it/s]
EPOCH 1800. Loss 342.77105712890625. Flow strength 4.316016674041748. Weight of flow 0.5 Heatmap of P embedding is 
 95%|█████████▍| 1899/2000 [03:01<00:08, 11.86it/s]
EPOCH 1900. Loss 404.10614013671875. Flow strength 4.311009883880615. Weight of flow 0.5 Heatmap of P embedding is 
100%|██████████| 2000/2000 [03:10<00:00, 10.48it/s]
Exiting training with loss  tensor(442.3307, device='cuda:0', grad_fn=<AddBackward0>)

{% endraw %} {% raw %}
dfe.visualize_points(labels)
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
{% endraw %}

Tilted Circle

{% raw %}
X, flow, labels = directed_circle(num_nodes=1000, radius=1, ytilt=np.pi/4)
plot_directed_3d(X, flow, labels, mask_prob=0.5)
{% endraw %} {% raw %}
X = torch.tensor(X)
flow = torch.tensor(flow)
X = X.float().to(device)
flow = flow.float().to(device)
{% endraw %} {% raw %}
dfe = DiffusionFlowEmbedder(X,
flow,
t=1,
sigma_graph=15,
sigma_embedding=15,
device=device, 
autoencoder_shape = [20,10],
flow_artist_shape=[30,20,10],
flow_strength_graph=2, 
flow_strength_embedding=5, 
learnable_flow_strength=True,
weight_of_flow = 0.5,
learning_rate=0.01
)
dfe = dfe.to(device)
embeddings = dfe.fit(n_steps=2000)
  0%|          | 0/2000 [00:00<?, ?it/s]
EPOCH 0. Loss 667.5437622070312. Flow strength 4.989999771118164. Weight of flow 0.5 Heatmap of P embedding is 
  5%|▌         | 100/2000 [00:03<01:05, 28.83it/s]
EPOCH 100. Loss 667.2559204101562. Flow strength 4.8423686027526855. Weight of flow 0.5 Heatmap of P embedding is 
 10%|█         | 200/2000 [00:07<01:02, 28.72it/s]
EPOCH 200. Loss 666.7958374023438. Flow strength 4.44754695892334. Weight of flow 0.5 Heatmap of P embedding is 
 15%|█▍        | 299/2000 [00:10<00:58, 28.86it/s]
EPOCH 300. Loss 666.6117553710938. Flow strength 3.6958649158477783. Weight of flow 0.5 Heatmap of P embedding is 
 20%|█▉        | 399/2000 [00:14<00:55, 29.10it/s]
EPOCH 400. Loss 666.5806274414062. Flow strength 3.005265712738037. Weight of flow 0.5 Heatmap of P embedding is 
 25%|██▌       | 500/2000 [00:18<00:53, 28.17it/s]
EPOCH 500. Loss 666.5630493164062. Flow strength 2.4203834533691406. Weight of flow 0.5 Heatmap of P embedding is 
 30%|██▉       | 599/2000 [00:21<00:49, 28.45it/s]
EPOCH 600. Loss 666.5516967773438. Flow strength 1.9887703657150269. Weight of flow 0.5 Heatmap of P embedding is 
 35%|███▍      | 698/2000 [00:25<00:46, 28.12it/s]
EPOCH 700. Loss 666.5470581054688. Flow strength 1.7977397441864014. Weight of flow 0.5 Heatmap of P embedding is 
 40%|████      | 800/2000 [00:29<00:42, 28.34it/s]
EPOCH 800. Loss 666.5475463867188. Flow strength 1.6819127798080444. Weight of flow 0.5 Heatmap of P embedding is 
 45%|████▍     | 899/2000 [00:33<00:39, 27.95it/s]
EPOCH 900. Loss 666.5535888671875. Flow strength 1.6262315511703491. Weight of flow 0.5 Heatmap of P embedding is 
 50%|████▉     | 998/2000 [00:36<00:34, 28.69it/s]
EPOCH 1000. Loss 666.55712890625. Flow strength 1.5920469760894775. Weight of flow 0.5 Heatmap of P embedding is 
 55%|█████▌    | 1100/2000 [00:40<00:33, 26.82it/s]
EPOCH 1100. Loss 666.551025390625. Flow strength 1.588066816329956. Weight of flow 0.5 Heatmap of P embedding is 
 60%|█████▉    | 1199/2000 [00:44<00:27, 28.66it/s]
EPOCH 1200. Loss 666.5385131835938. Flow strength 1.5636647939682007. Weight of flow 0.5 Heatmap of P embedding is 
 65%|██████▍   | 1298/2000 [00:47<00:25, 28.07it/s]
EPOCH 1300. Loss 666.5369262695312. Flow strength 1.5073577165603638. Weight of flow 0.5 Heatmap of P embedding is 
 70%|███████   | 1400/2000 [00:51<00:20, 28.96it/s]
EPOCH 1400. Loss 666.5347900390625. Flow strength 1.4788708686828613. Weight of flow 0.5 Heatmap of P embedding is 
 75%|███████▌  | 1500/2000 [00:55<00:17, 28.97it/s]
EPOCH 1500. Loss 666.5307006835938. Flow strength 1.4599934816360474. Weight of flow 0.5 Heatmap of P embedding is 
 80%|████████  | 1600/2000 [00:58<00:13, 28.75it/s]
EPOCH 1600. Loss 666.5322265625. Flow strength 1.4712326526641846. Weight of flow 0.5 Heatmap of P embedding is 
 85%|████████▌ | 1700/2000 [01:02<00:10, 29.02it/s]
EPOCH 1700. Loss 666.5331420898438. Flow strength 1.4765619039535522. Weight of flow 0.5 Heatmap of P embedding is 
 90%|████████▉ | 1799/2000 [01:06<00:07, 28.32it/s]
EPOCH 1800. Loss 666.5335693359375. Flow strength 1.4726852178573608. Weight of flow 0.5 Heatmap of P embedding is 
 95%|█████████▍| 1898/2000 [01:09<00:03, 28.19it/s]
EPOCH 1900. Loss 666.533203125. Flow strength 1.47734534740448. Weight of flow 0.5 Heatmap of P embedding is 
100%|██████████| 2000/2000 [01:13<00:00, 27.16it/s]
Exiting training with loss  tensor(666.5333, grad_fn=<AddBackward0>)

{% endraw %} {% raw %}
dfe.visualize_points(labels)
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
{% endraw %}

Cylinder

{% raw %}
X, flow, labels = directed_cylinder(num_nodes=1000, radius=1, height=20, xtilt=np.pi/4, ytilt=0)
plot_directed_3d(X, flow, labels, mask_prob=0.5)
{% endraw %} {% raw %}
X = torch.tensor(X)
flow = torch.tensor(flow)
X = X.float().to(device)
flow = flow.float().to(device)
{% endraw %} {% raw %}
dfe = DiffusionFlowEmbedder(X,flow,t=1,sigma_graph=1,sigma_embedding=1,flow_strength_graph=2

device=device)
dfe = dfe.to(device)
embeddings = dfe.fit(n_steps=2000)
100%|██████████| 2000/2000 [00:29<00:00, 67.56it/s]
Exiting training with loss  tensor(4005.0149, device='cuda:0', grad_fn=<AddBackward0>)

{% endraw %} {% raw %}
dfe.visualize_points(labels)
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
{% endraw %}

Flat Spiral

{% raw %}
X, flow, labels = directed_spiral(num_nodes=1000, num_spirals=2.5, radius=1)
plot_directed_2d(X, flow, labels)
{% endraw %} {% raw %}
X = torch.tensor(X)
flow = torch.tensor(flow)
X = X.float().to(device)
flow = flow.float().to(device)
{% endraw %} {% raw %}
dfe = DiffusionFlowEmbedder(X,flow,t=1,sigma_graph=10,sigma_embedding=15,
device=device,
learning_rate=0.001,
weight_of_flow=1)
dfe = dfe.to(device)
embeddings = dfe.fit(n_steps=10000)
  0%|          | 0/10000 [00:00<?, ?it/s]
EPOCH 0. Loss 255.82760620117188. Flow strength 4.999000072479248. Weight of flow 1 Heatmap of P embedding is 
  1%|          | 100/10000 [00:33<54:47,  3.01it/s]
EPOCH 100. Loss 254.57586669921875. Flow strength 5.101325988769531. Weight of flow 1 Heatmap of P embedding is 
  2%|▏         | 161/10000 [00:55<55:48,  2.94it/s]  
The Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.
Canceled future for execute_request message before replies were done
{% endraw %} {% raw %}
dfe.visualize_points(labels)
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
{% endraw %}

Tilted Spiral

{% raw %}
X, flow, labels = directed_spiral(num_nodes=100, num_spirals=2.5, radius=1, xtilt=np.pi/4)
plot_directed_3d(X, flow, labels, mask_prob=0.5)
{% endraw %} {% raw %}
X = torch.tensor(X)
flow = torch.tensor(flow)
X = X.float().to(device)
flow = flow.float().to(device)
{% endraw %} {% raw %}
dfe = DiffusionFlowEmbedder(X,flow,t=1,sigma_graph=15,sigma_embedding=15,device=device)
dfe = dfe.to(device)
embeddings = dfe.fit(n_steps=2000)
100%|██████████| 2000/2000 [00:28<00:00, 69.69it/s]
Exiting training with loss  tensor(191.4982, device='cuda:0', grad_fn=<AddBackward0>)

{% endraw %} {% raw %}
dfe.visualize_points(labels)
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
{% endraw %}

Swiss Roll

{% raw %}
X, flow, labels = directed_swiss_roll(num_nodes=1000, num_spirals=2.5, radius=1, height=20, xtilt=np.pi/4, ytilt=0)
plot_directed_3d(X, flow, labels, mask_prob=0.5)
{% endraw %} {% raw %}
X = torch.tensor(X)
flow = torch.tensor(flow)
X = X.float().to(device)
flow = flow.float().to(device)
{% endraw %} {% raw %}
dfe = DiffusionFlowEmbedder(X,flow,t=1,sigma_graph=15,sigma_embedding=15,device=device)
dfe = dfe.to(device)
embeddings = dfe.fit(n_steps=2000)
100%|██████████| 2000/2000 [00:29<00:00, 68.07it/s]
Exiting training with loss  tensor(2477.8306, device='cuda:0', grad_fn=<AddBackward0>)

{% endraw %} {% raw %}
dfe.visualize_points(labels)
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
{% endraw %} {% raw %}
import phate
phate_operator = phate.PHATE(n_components = 2, knn=5, decay=20, t=150)
phate_X = phate_operator.fit_transform(X.cpu())
phate.plot.scatter2d(phate_X, c=labels)
Calculating PHATE...
  Running PHATE on 1000 observations and 3 variables.
  Calculating graph and diffusion operator...
    Calculating KNN search...
    Calculated KNN search in 0.04 seconds.
    Calculating affinities...
    Calculated affinities in 0.02 seconds.
  Calculated graph and diffusion operator in 0.07 seconds.
  Calculating diffusion potential...
  Calculated diffusion potential in 0.17 seconds.
  Calculating metric MDS...
  Calculated metric MDS in 2.06 seconds.
Calculated PHATE in 2.32 seconds.
/tmp/ipykernel_223466/2472049010.py:4: DeprecationWarning: Call to deprecated function (or staticmethod) scatter2d. (Use scprep.plot.scatter2d instead) -- Deprecated since version 1.0.0.
  phate.plot.scatter2d(phate_X, c=labels)
<AxesSubplot:>
{% endraw %}

Uniform Swiss Roll

{% raw %}
X, flow, labels = directed_swiss_roll_uniform(num_nodes=1000, num_spirals=2.5, radius=1, height=20, xtilt=0, ytilt=0)
plot_directed_3d(X, flow, labels, mask_prob=0.5)
{% endraw %} {% raw %}
X = torch.tensor(X)
flow = torch.tensor(flow)
X = X.float().to(device)
flow = flow.float().to(device)
C:\Users\nhiin\AppData\Local\Temp\ipykernel_6980\3651268102.py:1: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  X = torch.tensor(X)
C:\Users\nhiin\AppData\Local\Temp\ipykernel_6980\3651268102.py:2: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  flow = torch.tensor(flow)
{% endraw %} {% raw %}
dfe = DiffusionFlowEmbedder(X,flow,t=1,sigma_graph=15,sigma_embedding=15,device=device)
dfe = dfe.to(device)
embeddings = dfe.fit(n_steps=2000)
{% endraw %} {% raw %}
dfe.visualize_points(labels)
{% endraw %}